/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.logging.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.groupme.model.Resource;
import net.groupme.model.Tag;
import net.groupme.search.strategy.model.WeightedResource;

/**
 * 
 * created on 4 Jan 2008
 * 
 * This class extends {@link WeightedResource} with a list of search strings that were
 * used to find the resource.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.1 $ $Date: 2008-01-09 19:14:04 $
 */
public class WeightedResourceAndSearchString extends WeightedResource {

	public List<String> searchStrings = null;
	/**
	 * Creates a new WeightedResourceAndSearchString instance.
	 * @param resource the resource
	 * @param weight a weight that is assigned to the resource
	 */
	public WeightedResourceAndSearchString(Resource resource, double weight) {
		super(resource, weight);
		searchStrings = new ArrayList<String>();
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String result = "score: " + weight + " - " +  resource.getName() + " (id: " + resource.getId() + ", type: " + resource.getType() +")";
		result += " - tags: ";
		List<String> tags = new ArrayList<String>();
		String keyword = null;
		for(Tag tag: resource.getTags()){
			keyword = tag.getKeyword();
			result += keyword + ", ";
			tags.add(keyword);
		}
		Map<String, Integer> tagsNum = new HashMap<String, Integer>();
		Double searchOperationsWithTagOfResource = 0.0;
		Double searchOperations = 0.0;
		for(String tag: searchStrings){
			if(tagsNum.containsKey(tag)){
				tagsNum.put(tag, tagsNum.get(tag) + 1);
			}else{
				tagsNum.put(tag, 1);
			}
			
			searchOperations++;
			if(tags.contains(tag)){
				searchOperationsWithTagOfResource++;
			}
		}
		result += "\nPercentage of Search Operations with tag of resource: " + (searchOperationsWithTagOfResource / searchOperations);
		
		
		result += "\nfound with: ";
		for(String key: tagsNum.keySet()){
			result+= key + " (" + tagsNum.get(key) + "), ";
		}
		result +="\n";
		return result;
	}
	
	public String toLatexString(){
		String result = "";
		String keyword = null;
		List<String> tags = new ArrayList<String>();
		for(Tag tag: resource.getTags()){
			keyword = tag.getKeyword();
			result += keyword + ", ";
			tags.add(keyword);
		}
		Map<String, Integer> tagsNum = new HashMap<String, Integer>();
		Double searchOperationsWithTagOfResource = 0.0;
		Double searchOperations = 0.0;
		for(String tag: searchStrings){
			if(tagsNum.containsKey(tag)){
				tagsNum.put(tag, tagsNum.get(tag) + 1);
			}else{
				tagsNum.put(tag, 1);
			}
			
			searchOperations++;
			if(tags.contains(tag)){
				searchOperationsWithTagOfResource++;
			}
		}
		String perc = "" + (1.0 -(searchOperationsWithTagOfResource / searchOperations));
		result = resource.getName() + " & " + resource.getType() + " & " + (perc.length() >= 6 ? (perc.substring(2, 4) + "." + perc.substring(4, 6)) : perc) + "\\% ";
		return result;
	}
		
}
